home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / DB / mssql.php < prev    next >
Encoding:
PHP Script  |  2001-03-06  |  6.4 KB  |  208 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: mssql.php,v 1.12 2000/09/13 11:27:59 ssb Exp $
  20. //
  21. // Database independent query interface definition for PHP's Microsoft SQL Server
  22. // extension.
  23. //
  24.  
  25. require_once 'DB/common.php';
  26.  
  27. class DB_mssql extends DB_common {
  28.  
  29.     var $connection;
  30.     var $phptype, $dbsyntax;
  31.     var $prepare_tokens = array();
  32.     var $prepare_types = array();
  33.  
  34.     function DB_mssql() {
  35.         $this->phptype = 'mssql';
  36.         $this->dbsyntax = 'mssql';
  37.         $this->features = array(
  38.             'prepare' => false,
  39.             'pconnect' => true,
  40.             'transactions' => false
  41.         );
  42.     }
  43.  
  44.     function connect (&$dsn, $persistent=false) {
  45.         if(is_array($dsn)) {
  46.             $dsninfo = &$dsn;
  47.         } else {
  48.             $dsninfo = DB::parseDSN($dsn);
  49.         }
  50.         if (!$dsninfo || !$dsninfo['phptype']) {
  51.             return $this->raiseError(); 
  52.         }
  53.  
  54.         $user = $dsninfo['username'];
  55.         $pw = $dsninfo['password'];
  56.         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  57.         $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';
  58.         if ($dbhost && $user && $pw) {
  59.             $conn = $connect_function($dbhost, $user, $pw);
  60.         } elseif ($dbhost && $user) {
  61.             $conn = $connect_function($dbhost,$user);
  62.         } else {
  63.             $conn = $connect_function($dbhost);
  64.         }
  65.         if ($dsninfo['database']) {
  66.             @mssql_select_db($dsninfo['database'], $conn);
  67.         } else {
  68.             return $this->raiseError();
  69.         }
  70.         $this->connection = $conn;
  71.         return DB_OK;
  72.     }
  73.  
  74.     function disconnect() {
  75.         return @mssql_close($this->connection);
  76.     }
  77.  
  78.     function &query( $stmt ) {
  79.     $this->last_query = $query;
  80.         $result = @mssql_query($stmt, $this->connection);
  81.         if (!$result) {
  82.             return $this->raiseError();
  83.         }
  84.         // Determine which queries that should return data, and which
  85.         // should return an error code only.
  86.         if (preg_match('/(SELECT|SHOW|LIST|DESCRIBE)/i', $stmt)) {
  87.             $resultObj = new DB_result($this, $result);
  88.             return $resultObj;
  89.         } else {
  90.             return DB_OK;
  91.         }
  92.     }
  93.  
  94.     function simpleQuery($stmt) {
  95.     $this->last_query = $query;
  96.         $result = @mssql_query($stmt, $this->connection);
  97.         if (!$result) {
  98.             return $this->raiseError();
  99.         }
  100.         // Determine which queries that should return data, and which
  101.         // should return an error code only.
  102.         if ( preg_match('/(SELECT|SHOW|LIST|DESCRIBE)/i', $stmt) ) {
  103.             return $result;
  104.         } else {
  105.             return DB_OK;
  106.         }
  107.     }
  108.  
  109.     function &fetchRow($result, $fetchmode=DB_FETCHMODE_DEFAULT) {
  110.     if ($fetchmode == DB_FETCHMODE_DEFAULT) {
  111.         $fetchmode = $this->fetchmode;
  112.     }
  113.         if ($fetchmode & DB_FETCHMODE_ASSOC) {
  114.             $row = @mssql_fetch_array($result);
  115.         } else {
  116.             $row = @mssql_fetch_row($result);
  117.         }
  118.         if (!$row) {
  119.             return $this->raiseError();
  120.         }
  121.         return $row;
  122.     }
  123.  
  124.     function fetchInto($result, &$ar, $fetchmode=DB_FETCHMODE_DEFAULT) {
  125.     if ($fetchmode == DB_FETCHMODE_DEFAULT) {
  126.         $fetchmode = $this->fetchmode;
  127.     }
  128.         if ($fetchmode & DB_FETCHMODE_ASSOC) {
  129.             $ar = @mssql_fetch_array($result);
  130.         } else {
  131.             $ar = @mssql_fetch_row($result);
  132.         }
  133.         if (!$ar) {
  134.             return $this->raiseError();
  135.         }
  136.         return DB_OK;
  137.     }
  138.  
  139.     function freeResult($result) {
  140.         if (is_resource($result)) {
  141.             return @mssql_free_result($result);
  142.         }
  143.         if (!isset($this->prepare_tokens[$result])) {
  144.             return false;
  145.         }
  146.         unset($this->prepare_tokens[$result]);
  147.         unset($this->prepare_types[$result]);
  148.         return true; 
  149.     }
  150.  
  151.     function numCols($result) {
  152.         $cols = @mssql_num_fields($result);
  153.         if (!$cols) {
  154.             return $this->raiseError();
  155.         }
  156.         return $cols;
  157.     }
  158.  
  159.     function prepare($query) {
  160.         $tokens = split('[\&\?]', $query);
  161.         $token = 0;
  162.         $types = array();
  163.         for ($i = 0; $i < strlen($query); $i++) {
  164.             switch ($query[$i]) {
  165.                 case '?':
  166.                     $types[$token++] = DB_PARAM_SCALAR;
  167.                     break;
  168.                 case '&':
  169.                     $types[$token++] = DB_PARAM_OPAQUE;
  170.                     break;
  171.             }
  172.         }
  173.         $this->prepare_tokens[] = &$tokens;
  174.         end($this->prepare_tokens);
  175.         $k = key($this->prepare_tokens);
  176.         $this->prepare_types[$k] = $types;
  177.         return $k;
  178.     }
  179.  
  180.     function execute($stmt, $data = false) {
  181.         $realquery = $this->execute_emulate_query($stmt, $data);
  182.     $this->last_query = $realquery;
  183.         $result = @mssql_query($realquery, $this->connection);
  184.         if (!$result) {
  185.             return $this->raiseError();
  186.         }
  187.         if ( preg_match('/(SELECT|SHOW|LIST|DESCRIBE)/i', $realquery) ) {
  188.             return $result;
  189.         } else {
  190.             return DB_OK;
  191.         }
  192.     }
  193.  
  194.     function autoCommit($onoff=false) {
  195.         return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  196.     }
  197.  
  198.     function commit() {
  199.         return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  200.     }
  201.  
  202.     function rollback() {
  203.         return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  204.     }
  205. }
  206.  
  207. ?>
  208.